<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2023/3/17
  Time: 13:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <form id="formId" method="post">
        <table>
            <tr>
                <td>用户名：</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>年龄：</td>
                <td><input type="text" name="age"/></td>
            </tr>
            <tr>
                <td>性别：</td>
                <td><input type="text" name="gender"/></td>
            </tr>
            <tr>
                <td>班级：</td>
                <td>
                    <select id="banjiId" name="banjiId">
                        <option>---请选择---</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><input type="button" onclick="submitForm()" value="添加"/></td>
                <td><input type="reset" value="重置"/></td>
            </tr>
        </table>
    </form>

    <script>
        $(function() {
            $.post(
                '${pageContext.request.contextPath}/banji?method=selectAll',
                function(jsonResult) {
                    //{"code":0,"msg":null,"data":[{"id":1,"name":"java1807"},{"id":2,"name":"java1812"},{"id":3,"name":"java230301"},{"id":4,"name":"aa"}],"ok":true}
                    //{"id":1,"name":"java1807"}
                    console.log(jsonResult);
                    $(jsonResult.data).each(function() {
                        // this: {"id":1,"name":"java1807"}
                        $('#banjiId').append('<option value="'+this.id+'">'+this.name+'</option>');
                    });

                    //form.render();

                },
                'json'
            );
        });

        function submitForm() {
            // {'name':'zhansgan','age':23,'banjiId':3}
            $.post(
                '${pageContext.request.contextPath}/student?method=add',
                $('#formId').serialize(),
                function(jsonResult) {
                    console.log(jsonResult);
                    if (jsonResult.ok) {
                        mylayer.okUrl(jsonResult.msg, '${pageContext.request.contextPath}/student?method=selectAll')
                    } else {
                        mylayer.errorMsg(jsonResult.msg);
                    }
                },
                'json'
            );
        }
    </script>
</body>
</html>
